package someTestExcemple.huaweiOd.slidingWindow;

//华为OD机试 - 阿里巴巴找黄金宝箱(V) - 滑动窗口（Java 2024 E卷 100分）
//https://blog.csdn.net/guorui_java/article/details/142289375
//求连续K个元素的最大值，典型滑动窗口题目
import java.util.*;
public class GoldChestV {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] boxes = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        int K = Integer.parseInt(sc.nextLine());
        int n = boxes.length;
        sc.close();
        int maxSum = Integer.MIN_VALUE;
        if(K>n) {
            System.out.println("error!");
            return;
        }
        int windowSum = 0;
        //初始化第一个窗口的sum
        for(int i=0;i<K;i++) {
            windowSum+=boxes[i];
        }
        maxSum = windowSum;
        //进行窗口滑动，直到右边界到数组末尾
        for(int i=K;i<n;i++) {
            windowSum = windowSum +boxes[i] -boxes[i-K];
            maxSum = Math.max(maxSum,windowSum);
        }
        System.out.println(maxSum);

    }
}
